//
//  DealerMineVc.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/14.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "DealerMineVc.h"
#import "Tools.h"
#import "config.h"
#import "PhootSelecteView.h"
#import "VPImageCropperViewController.h"
#import "DealerDataManage.h"
#import "MyOrderViewPageVc.h"
#import "UIButton+WebCache.h"
#import "UserInfoData.h"
#import "BdTV.h"
#import "BrokerManageVc.h"
#import "DealerEditInfoVc.h"

#import "SettingsViewController.h"
#import "TxtReadVc.h"
#import "StoreViewController.h"
#import "CommisstionVc.h"
#import "UserModal.h"
#import "StringTool.h"
#import "FinshOrderVC.h"
#import "CalculatorViewController.h"
#import "MessBtn.h"
#import "MessageVC.h"
#import "UIImageView+WebCache.h"
#import "UIView+circular.h"
#import "MessageDM.h"


@interface DealerMineVc ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,VPImageCropperDelegate>


@property (weak, nonatomic) IBOutlet UIView *headView;

@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UIImageView *headImg;

@property (weak, nonatomic) IBOutlet UILabel *companyName;


@property (nonatomic,strong)UserInfoData *data;
@property (weak, nonatomic) IBOutlet UIView *labView;


@property (weak, nonatomic) IBOutlet UILabel *orderCountLab;

@property (weak, nonatomic) IBOutlet UILabel *moneyCountLab;
@property (weak, nonatomic) IBOutlet UILabel *storeCountLab;


@property (weak, nonatomic) IBOutlet UILabel *brokerCountLab;


@property (weak, nonatomic) IBOutlet UIButton *dealBtn;

@property (weak, nonatomic) IBOutlet UIButton *commissionBtn;

@property (weak, nonatomic) IBOutlet UIButton *brokerBtn;

@property (weak, nonatomic) IBOutlet UIButton *storeBtn;

@property (weak, nonatomic) IBOutlet UIButton *settingBtn;


@property(nonatomic,assign) Boolean needRefresh;

@property (nonatomic,strong)PhootSelecteView *menuView;
@property DealerDataManage *dm;

@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (nonatomic,strong)MessBtn *messBtn;


-(void)headViewHandleSingleTap:(UITapGestureRecognizer*)sender;

-(void)upImg:(UIImage*)img;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIView *brokerView;



@end



@implementation DealerMineVc
@synthesize menuView,dm;



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    self.needRefresh = true;
}


-(void)refreshMsgNotReadCount{
    
    self.messBtn.badge = [NSString stringWithFormat:@"%ld",(long)[MessageDM getMessageNotReadCount]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.needRefresh) {
        [self refreshData];
    }

    self.needRefresh = false;
    
    self.messBtn.badge =  self.messBtn.badge = [NSString stringWithFormat:@"%ld",(long)[MessageDM getMessageNotReadCount]];;
}


-(void)initView{
    
    [super initView];
//    [self.headImg imgViewToCircular:self.headImg];
    [self.labView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.33]];
    [self.brokerView didMoveToSuperview];
    [self.brokerView didMoveToWindow];
    self.scrollview.showsVerticalScrollIndicator = NO;
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    
    self.messBtn = [MessBtn buttonWithType:UIButtonTypeCustom];
    [self.messBtn setFrame:CGRectMake(PMW-60,0,60, 44)];
    [self.messBtn setShowsTouchWhenHighlighted:YES];
    [self.messBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.messBtn setImage:[UIImage imageNamed:@"messbtn"] forState:UIControlStateNormal];
   
    [self.messBtn addTarget:self action:@selector(messBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [title addSubview:self.messBtn];

    [title.backBtn setTag:11];
    title.backBtn.hidden = self.isHidden;
    title.backBtn.enabled = !self.isHidden;
    
    title.backBtn.hidden = YES;
    title.backBtn.enabled = NO;

    [[title doneBtn]removeFromSuperview];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    [title setMianTitle:@"我的"];
    
    
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewHandleSingleTap:)];
    [self.headView addGestureRecognizer:singleTap];
    
    
    dm = [[DealerDataManage alloc]init];
    
}

-(void)messBtnOnClick{
    
    MessageVC *vc = [MessageVC new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)refreshData{
    [super refreshData];
    
    
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
       ResulteMod *mod = dm.refreshUserInfo;
        
       
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            
            if (mod.resltState ==ResultFail) {
            
                [MBProgressHUD showError:[NSString stringWithFormat:@"网络出错:%@",mod.resultCode]];
                
            }else if (mod.resltState ==ResultNetError){
            
                
                [MBProgressHUD showError:@"网络出错"];
                
            }else{
           
                
               [self upUI];;
                
            }

        });
    });
}



-(void)upUI{
    
    
    UserInfoData *data = [DealerDataManage getUserInfo];
    
            
//    [self.headBtn setBackgroundColor:NoImgColor];
//    
//    [self.headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:data.headImgUrl] forState:UIControlStateNormal placeholderImage:nil];
    
    [self.headImg setBackgroundColor:NoImgColor];
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:data.headImgUrl] placeholderImage:[UIImage imageNamed:@"headtop_pic"]];
    
    
    [self.companyName setText:data.companyName];
    
    [self.orderCountLab setText:[NSString stringWithFormat:@"%ld",(long)data.ordersCount]];
    
    [self.moneyCountLab setText:data.moneyCount];
    
    [self.storeCountLab setText:[NSString stringWithFormat:@"%ld",(long)data.storesCount]];
    
    [self.brokerCountLab setText:[NSString stringWithFormat:@"%ld",(long)data.brokersCount]];
    
    
    if (![StringTool isBlankString:data.mobile]) {
         [self.phoneLab setText:data.mobile];
    }
   

    
}



-(void)headViewHandleSingleTap:(UITapGestureRecognizer*)sender{
    
   self.needRefresh = true;
  DealerEditInfoVc *vc = [[DealerEditInfoVc alloc]initWithNibName:@"DealerEditInfoVc" bundle:nil];
     vc.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:vc animated:YES];
}



- (IBAction)headViewOnClick:(UIButton *)sender {
    
    if (self.menuView==nil) {
        menuView = [PhootSelecteView creadInstance];
        [menuView.tackPicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [menuView.selectePicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    [menuView showInView:self.view];
}




- (IBAction)dealBtnOnClick:(id)sender {
    
    MyOrderViewPageVc *vc = [MyOrderViewPageVc new];
    vc.isHidden  = NO;
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)commisstionBtnOnClick:(id)sender {
    CommisstionVc *vc = [CommisstionVc new];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 经纪人管理
- (IBAction)brokerBtnOnClick:(id)sender {
    
    self.needRefresh = true;
    BrokerManageVc *vc = [BrokerManageVc new];
    vc.isHidden = NO;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)storeBtnOnClick:(id)sender {
    
    self.needRefresh = true;
    StoreViewController *vc = [StoreViewController new];
    vc.isHidden = NO;
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 系统设置
- (IBAction)settingBtnOnClick:(id)sender {
    
    SettingsViewController *vc = [SettingsViewController new];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}



-(void)btnOnclick:(UIButton*)sender{
    
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender==menuView.tackPicBtn) {
        
        // 跳转到相机
        NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
        
    }else if(sender==menuView.selectePicBtn){
    
        // 跳转到相册
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}



//上传头像
-(void)upImg:(UIImage*)img{

    [menuView hide];
    
    [dm upHeadImg:img View:self.headBtn];
}




#pragma mark  UiPickView delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    image = [Tools fixOrientation:image];

    int h = [Tools getHightDifPMsW:100/2 sh:100/2 nw:PMW];
    CGRect buf = CGRectMake(0, PMH/2-h/2,PMW, h);
    
    // 裁剪
    VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:image cropFrame:buf limitScaleRatio:6];
    imgEditorVC.delegate = self;
    [self presentViewController:imgEditorVC animated:YES completion:^{}];

}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}





#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)image {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
    
    image = [Tools imageWithImageSimple:image scaledToSize:CGSizeMake(100, 100)];
    
    [self upImg:image];
}



- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
}

#pragma mark 已完成订单
- (IBAction)finishOrder {
    FinshOrderVC *vc = [FinshOrderVC new];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 房贷计算器
- (IBAction)mortgageCalculator:(id)sender {
    CalculatorViewController *vc = [CalculatorViewController new];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}



@end
